<template>
  <div class="oper">
    <div class="title-line" content-position="left">流程处理</div>
    <p-table :show-header="false" bordered :pagination="false" :columns="columns" :datp-source="tableData">
      <div slot="name" slot-scope="row">{{ row.name }} </div>
      <div slot="area" slot-scope="row">
        <div class="area" v-if="row.area === 1">
          <p-radio-group v-model="radio">
            <p-radio :value="3">
              同意
            </p-radio>
            <p-radio :value="6">
              驳回
            </p-radio>
            <p-radio @click.native="toClick" :value="9">
              转办
            </p-radio>
          </p-radio-group>
        </div>
        <div style="display: flex; justify-content: space-between;" v-if="row.area === 2">
          <p-input
            v-model="comment"
            type="textarea"
            placeholder="请输入内容"
            resize="none"
            style="width: calc( 100% - 72px)"
          />
          <p-button @click="submit" :loading="cLoading" class="button" type="primary">提交</p-button>
        </div>
      </div>
    </p-table>
    <lookOne :modal="true" :title="'转办'" ref="lookOne"></lookOne>
  </div>
</template>

<script>
import Bus from '@/utils/bus'
import lookOne from '@/components/process/platform/pc/lookOne.vue'
import process from '@/api/process'
import oaConfig from '@/config/oa-config'

const columns = [
  {
    title: '创建时间',
    key: 'name',
    ellipsis: true,
    align: 'center',
    scopedSlots: { customRender: 'name' },
  },
  {
    title: '处理时间',
    key: 'area',
    ellipsis: true,
    scopedSlots: { customRender: 'area' },
  },
]

export default {
  props: {
    taskId: { type: String, default: '' },
    step: { type: String, default: '' },
  },
  components: {
    lookOne,
  },
  data () {
    return {
      columns,
      cLoading: false,
      radio: 3,
      comment: '',
      tableData: [
        {
          key: '1',
          name: '操作',
          area: 1,
        },
        {
          name: '处理意见',
          key: '2',
          area: 2,
        },
      ]
    }
  },
  methods: {
    toClick () {
      this.$refs.lookOne.startVisible = true
    },
    submitData () {
      const params = {
        // 处理意见
        comment: this.comment,
        // 当前任务ID
        taskId: this.taskId,
        assigneeId: '',
        variables: {}
      }
      if (this.radio == 3) {
        process.completeTask(params).then(res => {
          if (res.code === 0) {
            this.$message.success('同意操作提交成功')
            this.afterSubmit()
          } else {
            this.$message.error(res.msg)
          }
          this.cLoading = false
        }).catch(r => {
          this.cLoading = false
        })
      } else if (this.radio == 6) {
        process.rejectTask(params).then(res => {
          if (res.code === 0) {
            this.$message.success('驳回操作提交成功')
            this.afterSubmit()
          } else {
            this.$message.error(res.msg)
          }
          this.cLoading = false
        }).catch(r => {
          this.cLoading = false
        })
      } else if (this.radio == 9) {
        if (this.$refs.lookOne.curItem.length === 0) {
          this.$message.error('请选择转办的人')
          this.cLoading = false
          return
        }
        params.assigneeId = this.$refs.lookOne.curItem[0].uid
        process.transferTask(params).then(res => {
          if (res.code === 0) {
            this.$message.success('转办操作提交成功')
            this.afterSubmit()
          } else {
            this.$message.error(res.msg)
          }
          this.cLoading = false
        }).catch(r => {
          this.cLoading = false
        })
      }
    },
    submit () {
      const that = this
      this.cLoading = true
      // 通知表单2保存数据
      if (this.step && (this.step === 'b')) {
        Bus.$emit('prevSubmit', this.radio)
      }
      if (this.step && (this.step === 'c')) {
        this.submitData()
      }
    },
    afterSubmit () {
      // window.opener = null;
      // window.open('', '_self');
      // window.location.href="about:blank";
      // window.close();
      if (window.opener && window.opener.dispatchEvent) {
          window.opener.dispatchEvent(new Event('getTableData'))
      } else {
          window.opener.fireEvent(new Event('getTableData'))
      }
      window.location.href = oaConfig.baseUrl + '/workflow/request/ViewRequestOS.jsp?reflush=1'
    }
  },
  mounted () {
    Bus.$on('nextSubmit', () => {
      console.log('可以真正提交了')
      this.submitData()
    })
  }
}
</script>

<style scoped lang="less">
.oper{
  overflow: hidden;
}
 .left {
    float: left;
    width: 94px;
    padding-left: 24px;
    font-size: 14px;
    border: 1px solid rgba(226, 226, 226, 1);
  }

  .right {
    width: 890px;
    display: inline-block;
    font-size: 14px;
    border: 1px solid rgba(226, 226, 226, 1);
  }

  .right .el-radio-group {
    margin-left: 10px;
  }

  .inputArea {
    float: left;
  }

  .el-textarea__inner {
    height: 64px;
  }

  .button {
    // width: 62px;
    margin-left: 4px;
    height: 54px;
    background-color: #409EFD;
    text-align: center;
    cursor: pointer;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    // border-radius: 2px;
  }
  .stepName {
    margin: 36px 0px 24px 0;
    font-size: 16px;
  }
  .submitBtn{
    cursor: pointer;
    color: #fff;
  }
  .title-line{
    font-size: 16px;
    margin: 30px 0 10px 0;
  }
  .area{
    height: 40px;
    display: flex;
    align-items: center;
  }
</style>
